import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;


public class Server{
	private static final int port = 7789;
	private Database db;
	private ServerSocket sock;
	private ThreadPool pool;
	public static HashMap<Integer, Station> stations = new HashMap<Integer, Station>();
	
	
	public Server() {
		db = new Database("jdbc:mysql://localhost/unwdmi","root","");
		
		pool = new ThreadPool(1601);
		
    	listen();
	}
	
    public void listen() {
    	try {
    		sock = new ServerSocket(port);
    		Socket clientSocket;
    		
//			StationCollector stationCollector = new StationCollector(db);
//			
//    		pool.add(stationCollector);
    		
    		
    		// Listen for connection
    		while(true) {	  
    			clientSocket = sock.accept();
    			WriteThread writeThread = new WriteThread(db);
    			pool.add(writeThread);
    			ClientThread clientThread = new ClientThread(clientSocket,db,writeThread);       		
    			pool.add(clientThread);
    			
    			
    		}
	    } catch (IOException e){
	    	System.err.println(e);
	    }
    }

}
